import io
import os

import pandas as pd
from openai import OpenAI
if __name__ == '__main__':

    def ChenMingStr():
        dataFrame = pd.DataFrame({'x1':[1,2],'x2':[3,4]})
        return dataFrame.to_string()

    def ChenMingJson():
        dataFrame = pd.DataFrame({'x1':[1,2],'x2':[3,4]})
        return dataFrame.to_json(orient='records')

    #字符串格式
    messages = [
        {"role":"system","content":"您是一个数据分析卖家，请解答用户的疑问。"},
        {"role": "user", "content": "请解决一个这一组数据：" + ChenMingStr()},
    ]

    client = OpenAI(api_key=os.environ["OPENAI_API_KEY_ZHIHU"],base_url=os.environ["OPENAI_API_BASE_ZHIHU"])
    response = client.chat.completions.create(messages=messages,model="gpt-4o",temperature=0)
    print(response.choices[0].message.content)

    resultStr = ChenMingStr()
    print(resultStr)
    data = io.StringIO(resultStr)
    dataFrame = pd.read_csv(data,sep='\s+',index_col = 0)
    print(dataFrame)

    # 字符串格式
    messages = [
        {"role": "system", "content": "您是一个数据分析卖家，请解答用户的疑问。"},
        {"role": "user", "content": "请解决一个这一组数据：" + ChenMingJson()},
    ]
    client = OpenAI(api_key=os.environ["OPENAI_API_KEY_ZHIHU"],base_url=os.environ["OPENAI_API_BASE_ZHIHU"])
    response = client.chat.completions.create(messages=messages,model="gpt-4o",temperature=0)
    print(response.choices[0].message.content)

    resultStr = ChenMingJson()
    print(resultStr)
    data = io.StringIO(resultStr)
    dataFrame = pd.read_json(data)
    print(dataFrame)

